<?php
/*
* Created on 18/05/2006
*
* @author Rafael Guerrero
*/
class TransSetListHandler extends BaseHandler {
	/**
	* @type array
	*/
	private $nodesByName;
	/**
	* @type array
	*/
	private $fieldByName;
	
//	/**
//	* @type array
//	*/
//	private $variantByName;
	
	/**
	* @type array
	*/
	private $fieldStateByName;

	/**
	* @type MTransSet
	*/
	private $currTransSet;

	/**
	* @type bool
	*/
	private $editNotice;

	// ----------------------------------------
	// Props
	// ----------------------------------------

	/**
	* @type array
	*/
	private $transSetList;

	// ----------------------------------------

	public function __construct(& $nodesByName, & $fieldByName) {
		$this->nodesByName = & $nodesByName;
		$this->fieldByName = & $fieldByName;
		//$this->variantByName = & $variantByName;
	}

	// ----------------------------------------
	// Handler Methods
	// ----------------------------------------

	private function transSetBeg(& $parser, $tag, $attributes) {
		$this->currTransSet = & BaseHandler :: $session->create('MTransSet');
		$this->currTransSet->setName($attributes['name']);
		$this->transSetList[] = & $this->currTransSet;
		$this->fieldStateByName = array ();

		foreach ($this->fieldByName as $key => & $docField) {

			$docFieldState = & BaseHandler :: $session->create('MDocfieldState');
			$docFieldState->setState(MDocfieldState :: $STATE_HIDDEN);
			$docFieldState->setType(MDocfieldState :: $TYPE_TRANS_SET);

			//docField . getDocFieldStateList() . add(docFieldState);
			$docFieldState->setDocFieldRef($docField);

			//currTransSet . getDocFieldStateList() . add(docFieldState);
			$docFieldState->setTransSetRef($this->currTransSet);

			$this->fieldStateByName[$docField->getName()] = & $docFieldState;
		}
	}

	private function editNoticeBeg(& $parser, $tag, $attributes) {
		$this->editNotice = TRUE;
	}

	private function captionEnd(& $parser, $tag) {
		if ($this->editNotice) {
			$this->currTransSet->setEditNoticeCaption($this->text);
		} else {
			$this->currTransSet->setCaption($this->text);
		}
	}

	private function transListEnd(& $parser, $tag) {

		$transList = explode(',', $this->text);

		for ($i = 0; $i < count($transList); $i++) {
			if (!isset ($this->nodesByName[$transList[$i]])) {
				throw new Exception("Trans node not found: " . $transList[$i]);
			}
			$trans = & $this->nodesByName[$transList[$i]];
			//currTransSet . getTransList() . add(trans);
			$trans->setTransSetRef($this->currTransSet);
			$roleTransSet = $trans->roleRef;
			$transSetType = $trans->type;
		}
		$this->currTransSet->roleRef = $roleTransSet;
		$this->currTransSet->transSetType = $transSetType;

	}

	private function editNoticeEnd(& $parser, $tag) {
		$this->editNotice = false;
	}

	private function mandatoryEnd(& $parser, $tag) {
		$transList = explode(',', $this->text);

		// Check if trans exists
		for ($i = 0; $i < count($transList); $i++) {
			if (!isset ($this->nodesByName[$transList[$i]])) {
				throw new Exception("Trans node not found: " . $transList[$i]);
			}
		}

		$this->currTransSet->setEditNoticeMandatory($this->text);
	}

	private function showNoticeEnd(& $parser, $tag) {
		$transList = explode(',', $this->text);

		// Check if trans exists
		for ($i = 0; $i < count($transList); $i++) {
			if (!isset ($this->nodesByName[$transList[$i]])) {
				throw new Exception("Trans node not found: " . $transList[$i]);
			}
		}

		$this->currTransSet->setShowNoticeTransList($this->text);
	}

	private function visibleDocFieldEnd(& $parser, $tag) {
		$fieldList = explode(',', $this->text);
		
		for ($i = 0; $i < count($fieldList); $i++) {

			if (!isset ($this->fieldStateByName[$fieldList[$i]])) {
				throw new Exception("Field not found: " . $fieldList[$i]);
			}
			$docFieldState = & $this->fieldStateByName[$fieldList[$i]];
			$docFieldState->setState(MDocfieldState :: $STATE_VISIBLE);
		}
	}

	private function readonlyDocFieldEnd(& $parser, $tag) {
		$fieldList = explode(',', $this->text);

		for ($i = 0; $i < count($fieldList); $i++) {
			if (!isset ($this->fieldStateByName[$fieldList[$i]])) {
				throw new Exception("Field not found: " . $fieldList[$i]);
			}

			$docFieldState = & $this->fieldStateByName[$fieldList[$i]];
			$docFieldState->setState(MDocfieldState :: $STATE_READONLY);
		}
	}
	
	
	private function agentClassEnd (& $parser, $tag) {
		$this->currTransSet->agentClass = $this->text;
	}	
	
	private function agentMethodsEnd (& $parser, $tag) {
		$this->currTransSet->agentMethods = $this->text;
	}

	private function userSupervisionClassEnd (& $parser, $tag) {
		$this->currTransSet->userSupervisionClass = $this->text;
	}

	private function userSupervisionMethodsEnd (& $parser, $tag) {
		$this->currTransSet->userSupervisionMethods  = $this->text;
	}
	
	private function delegatorClassEnd (& $parser, $tag) {
		$this->currTransSet->delegatorClass = $this->text;
	}
	
	private function delegatorMethodsEnd (& $parser, $tag) {
		$this->currTransSet->delegatorMethods = $this->text;
	}

	private function processClassEnd (& $parser, $tag) {
		$this->currTransSet->processClass = $this->text;
	}

	private function processMethodsEnd (& $parser, $tag) {
		$this->currTransSet->processMethods = $this->text;
	}
	
	
	// ----------------------------------------
	// BaseHandler Methods
	// ----------------------------------------

	public function startElement(& $parser, $tag, $attributes) {
		parent :: startElement($parser, $tag, $attributes);
		switch ($tag) {
			case 'transSet' :
				$this->transSetBeg($parser, $tag, $attributes);
				break;
			case 'editNotice' :
				$this->editNoticeBeg($parser, $tag, $attributes);
				break;
		}
	}

	public function endElement(& $parser, $tag) {
		parent :: endElement($parser, $tag);

		switch ($tag) {
			case 'caption' :
				$this->captionEnd($parser, $tag);
				break;
			case 'transList' :
				$this->transListEnd($parser, $tag);
				break;
			case 'editNotice' :
				$this->editNoticeEnd($parser, $tag);
				break;
			case 'mandatory' :
				$this->mandatoryEnd($parser, $tag);
				break;
			case 'showNotice' :
				$this->showNoticeEnd($parser, $tag);
				break;
			case 'visibleDocField' :
				$this->visibleDocFieldEnd($parser, $tag);
				break;
			case 'readonlyDocField' :
				$this->readonlyDocFieldEnd($parser, $tag);
				break;
			case 'agentClass' :
				$this->agentClassEnd($parser, $tag);
				break;
			case 'agentMethods' :
				$this->agentMethodsEnd($parser, $tag);
				break;
			case 'delegatorClass' :
				$this->delegatorClassEnd($parser, $tag);
				break;
			case 'delegatorMethods' :
				$this->delegatorMethodsEnd($parser, $tag);
				break;
			case 'processClass' :
				$this->processClassEnd($parser, $tag);
				break;
			case 'processMethod' :
				$this->processMethodsEnd($parser, $tag);
				break;
			case 'userSupervisionClass' :
				$this->userSupervisionClassEnd($parser, $tag);
				break;
			case 'userSupervisionMethods' :
				$this->userSupervisionMethodsEnd($parser, $tag);
				break;
		}
	}

	// ----------------------------------------
	// Props Methods
	// ----------------------------------------

	public function getTransSetList() {
		return $this->transSetList;
	}
}
?>
